﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.View;

namespace Benefit.Service.View
{
    /// <summary>
    /// 交易员五日均值
    /// </summary>
    public class ServiceOperatorFiveDayAvg : Benefit.Interface.View.IOperatorFiveDayAvg
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorFiveDayAvg(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        public void InitOperatorFiveDayAvg(int tradeHistoryId)
        {
            List<Models.Sys.Operator> operators = db.Operators.ToList();
            foreach (Models.Sys.Operator op in operators)
            {
                SetOperatorFiveDayAvg(op.Id, tradeHistoryId);
            }
            db.SaveChanges();
        }

        public void Delete(int tradeHistoryId)
        {
            var query = db.OperatorFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
            foreach (Models.View.OperatorFiveDayAvg avg in query)
            {
                db.OperatorFiveDayAvg.Remove(avg);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 获取交易员查询日期之前的五日均值
        /// </summary>
        /// <param name="operatorId"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public OperatorFiveDayAvg GetOperatorFiveDayAvg(int operatorId, int tradeHistoryId)
        {
            var query = db.OperatorFiveDayAvg.Where(a => a.OperatorId == operatorId).Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
            if (query.Count > 0)
            {
                return query.First();
            }
            else
            {
                return new OperatorFiveDayAvg();
            }
        }


        /// <summary>
        /// 初始化交易员五日均值(这个方法应该没问题)
        /// </summary>
        /// <returns></returns>
        public void SetOperatorFiveDayAvg(int operatorId, int tradeHistoryId)
        {
            OperatorFiveDayAvg change = new OperatorFiveDayAvg();
            change.OperatorId = operatorId;
            change.TradeHistoryId = tradeHistoryId;
            Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetPdateFromTradeHistoryId(tradeHistoryId), operatorId);
            if (accounts.Count > 0)
            {
                foreach (Models.Sys.Account acc in accounts)
                {
                    var queryChange = db.AccountFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.AccountId == acc.Id);
                    if (queryChange.Count() > 0)
                    {
                        change.BillCount += queryChange.First().BillCount;
                        change.DayCount += queryChange.First().DayCount;
                        change.Free += queryChange.First().Free;
                        change.LoseCount += queryChange.First().LoseCount;
                        change.OperatorId = operatorId;
                        change.Profit += queryChange.First().Profit;
                        change.SumLost += queryChange.First().SumLost;
                        change.SumWin += queryChange.First().SumWin;
                        change.TradeHistoryId = tradeHistoryId;
                        change.UsedMargin += queryChange.First().UsedMargin;
                        change.WinCount += queryChange.First().WinCount;
                        change.AvgBillBenefit += queryChange.First().AvgBillBenefit;
                        change.AvgPostionTime += queryChange.First().AvgPostionTime;
                        change.MaxReturn += queryChange.First().MaxReturn;
                        change.OverStopLoss += queryChange.First().OverStopLoss;
                        change.PingJinPing += queryChange.First().PingJinPing;
                        change.RaiseRate += queryChange.First().RaiseRate;
                        change.RiskCount += queryChange.First().RiskCount;
                        change.Utilization += queryChange.First().Utilization;
                        change.WinRate += queryChange.First().WinRate;
                        change.Yield += queryChange.First().Yield;

                    }
                }
                change.BillCount = Math.Round(change.BillCount / accounts.Count(), 2);
                change.DayCount = Math.Round(change.DayCount / accounts.Count(), 2);
                change.Free = Math.Round(change.Free / accounts.Count(), 2);
                change.LoseCount = Convert.ToInt32(change.LoseCount / accounts.Count());
                change.Profit = Math.Round(change.Profit / accounts.Count(), 2);
                change.SumLost = Math.Round(change.SumLost / accounts.Count(), 2);
                change.SumWin = Math.Round(change.SumWin / accounts.Count(), 2);
                change.UsedMargin = Math.Round(change.UsedMargin / accounts.Count(), 2);
                change.WinCount = Convert.ToInt32(change.WinCount / accounts.Count());
                change.AvgBillBenefit = Math.Round(change.AvgBillBenefit / accounts.Count(), 2);
                change.AvgPostionTime = Math.Round(change.AvgPostionTime / accounts.Count(), 2);

                change.OverStopLoss = Math.Round(change.OverStopLoss / accounts.Count(), 2);
                change.PingJinPing = Convert.ToInt32(change.PingJinPing / accounts.Count());
                change.RaiseRate = Math.Round(change.RaiseRate / accounts.Count(), 2);
                change.RiskCount = Convert.ToInt32(change.RiskCount / accounts.Count());
                change.Utilization = Math.Round(change.Utilization / accounts.Count(), 2);
                change.WinRate = Math.Round(change.WinRate / accounts.Count(), 2);
                change.Yield = Math.Round(change.Yield / accounts.Count(), 2);


            }

            db.OperatorFiveDayAvg.Add(change);


        }
    }
}
